/*     */ package dm.sql;
/*     */ 
/*     */ import dm.jdbc.dataConvertion.Convertion;
/*     */ import dm.jdbc.dbaccess.Const;
/*     */ import dm.jdbc.dbaccess.DBError;
/*     */ import dm.jdbc.util.StringUtil;
/*     */ import java.sql.SQLException;
/*     */ import java.sql.Time;
/*     */ import java.sql.Timestamp;
/*     */ import java.util.ResourceBundle;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DmdbTime
/*     */   implements Cloneable
/*     */ {
/*  32 */   final int DMDT_HOUR_POS = 0;
/*     */   
/*  34 */   final int DMDT_HOUR_LEN = 5;
/*     */   
/*  36 */   final int DMDT_MIN_POS = 5;
/*     */   
/*  38 */   final int DMDT_MIN_LEN = 6;
/*     */   
/*  40 */   final int DMDT_SEC_POS = 11;
/*     */   
/*  42 */   final int DMDT_SEC_LEN = 6;
/*     */   
/*  44 */   final int DMDT_MSEC_POS = 17;
/*     */   
/*  46 */   final int DMDT_MSEC_LEN = 20;
/*     */   
/*  48 */   byte[] times = new byte[5];
/*     */   
/*  50 */   int prec = 0;
/*     */   
/*     */   public DmdbTime() {}
/*     */   
/*     */   public final Object clone()
/*     */   {
/*  56 */     DmdbTime o = null;
/*     */     try {
/*  58 */       o = (DmdbTime)super.clone();
/*  59 */       if (this.times != null)
/*  60 */         o.times = ((byte[])this.times.clone());
/*  61 */       o.prec = this.prec;
/*     */     } catch (CloneNotSupportedException e) {
/*  63 */       e.printStackTrace();
/*     */     }
/*     */     
/*  66 */     return o;
/*     */   }
/*     */   
/*     */   public int hashCode() {
/*  70 */     return toString().hashCode();
/*     */   }
/*     */   
/*     */   public boolean equals(Object o) {
/*  74 */     if (o == null) {
/*  75 */       return false;
/*     */     }
/*  77 */     if (!(o instanceof DmdbTime)) {
/*  78 */       return false;
/*     */     }
/*  80 */     DmdbTime dbtm = (DmdbTime)o;
/*     */     
/*  82 */     byte[] bs = dbtm.times;
/*  83 */     for (int i = 0; i < 5; i++) {
/*  84 */       if (this.times[i] != bs[i]) {
/*  85 */         return false;
/*     */       }
/*     */     }
/*  88 */     return true;
/*     */   }
/*     */   
/*     */   public DmdbTime(byte[] bt, int prec) {
/*  92 */     this.times = bt;
/*  93 */     this.prec = prec;
/*     */   }
/*     */   
/*     */   public DmdbTime(String str, int prec) throws SQLException {
/*  97 */     this.prec = prec;
/*     */     
/*  99 */     String[] tmp = str.split(" ");
/* 100 */     if (tmp.length == 2) {
/* 101 */       str = tmp[1];
/*     */     }
/*     */     
/* 104 */     String[] subStr = StringUtil.split(str, ":.");
/*     */     try {
/* 106 */       byte h = Byte.parseByte(subStr[0]);
/* 107 */       byte m = Byte.parseByte(subStr[1]);
/* 108 */       byte s = Byte.parseByte(subStr[2]);
/* 109 */       int nano = 0;
/* 110 */       if (subStr.length > 3) {
/* 111 */         StringBuffer strNano = new StringBuffer(10);
/* 112 */         strNano.append("0");
/* 113 */         strNano.append(".");
/* 114 */         strNano.append(subStr[3]);
/* 115 */         double d_v = Double.valueOf(strNano.toString()).doubleValue();
/* 116 */         nano = (int)(d_v * 1000000.0D);
/*     */       }
/*     */       
/* 119 */       setBit(this.times, 0, 5, h);
/* 120 */       setBit(this.times, 5, 6, m);
/* 121 */       setBit(this.times, 11, 6, s);
/* 122 */       setBit(this.times, 17, 20, nano);
/*     */     } catch (Exception e) {
/* 124 */       DBError.throwSQLException(6007);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public final byte[] getByteArrayValue()
/*     */     throws SQLException
/*     */   {
/* 136 */     String str = getTimeWithNano();
/* 137 */     byte[] dt = new byte[12];
/* 138 */     String[] subStr = StringUtil.split(str, ":.");
/* 139 */     byte hour = Byte.parseByte(subStr[0]);
/* 140 */     byte minute = Byte.parseByte(subStr[1]);
/* 141 */     byte sec = Byte.parseByte(subStr[2]);
/* 142 */     if ((hour > 24) || (hour < 0) || (minute > 60) || (minute < 0) || (sec > 60) || 
/* 143 */       (sec < 0)) {
/* 144 */       throw new SQLException(
/* 145 */         Const.res.getString("error.invalidDateTimeTypeFormat"));
/*     */     }
/* 147 */     dt[4] = hour;
/* 148 */     dt[5] = minute;
/* 149 */     dt[6] = sec;
/*     */     
/* 151 */     int nano = 0;
/* 152 */     if (subStr.length > 3) {
/* 153 */       StringBuffer strNano = new StringBuffer(10);
/* 154 */       strNano.append("0");
/* 155 */       strNano.append(".");
/* 156 */       strNano.append(subStr[3]);
/* 157 */       double d_v = Double.valueOf(strNano.toString()).doubleValue();
/* 158 */       nano = (int)(d_v * 1000000.0D);
/*     */     }
/* 160 */     byte[] msec = Convertion.IntToByteArray(nano);
/* 161 */     System.arraycopy(msec, 0, dt, 7, 3);
/*     */     
/* 163 */     return dt;
/*     */   }
/*     */   
/*     */   public final byte getHour() {
/* 167 */     int b = getBit(this.times, 0, 5);
/* 168 */     return (byte)b;
/*     */   }
/*     */   
/*     */   public final byte getMinute() {
/* 172 */     int b = getBit(this.times, 5, 6);
/* 173 */     return (byte)b;
/*     */   }
/*     */   
/*     */   public final byte getSecond() {
/* 177 */     int b = getBit(this.times, 11, 6);
/* 178 */     return (byte)b;
/*     */   }
/*     */   
/*     */   public final int getNano() {
/* 182 */     int b = getBit(this.times, 17, 20);
/* 183 */     return b;
/*     */   }
/*     */   
/*     */   public final Time getTime() {
/* 187 */     return new Time(getHour(), getMinute(), getSecond());
/*     */   }
/*     */   
/*     */   public final String getTimeWithoutNano() {
/* 191 */     int hour = getHour();
/* 192 */     int min = getMinute();
/* 193 */     int sec = getSecond();
/*     */     
/* 195 */     StringBuffer sb = new StringBuffer(15);
/*     */     
/* 197 */     String h = new Integer(hour).toString();
/* 198 */     String m = new Integer(min).toString();
/* 199 */     String s = new Integer(sec).toString();
/*     */     
/* 201 */     if (h.length() < 2) {
/* 202 */       h = "0" + h;
/*     */     }
/*     */     
/* 205 */     if (m.length() < 2) {
/* 206 */       m = "0" + m;
/*     */     }
/*     */     
/* 209 */     if (s.length() < 2) {
/* 210 */       s = "0" + s;
/*     */     }
/*     */     
/* 213 */     sb.append(h).append(":").append(m).append(":").append(s);
/* 214 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public final String getTimeWithNano() {
/* 218 */     int nano = getNano();
/*     */     
/* 220 */     StringBuffer sb = new StringBuffer(15);
/*     */     
/* 222 */     String n = new Integer(nano).toString();
/*     */     
/* 224 */     String naro = "";
/* 225 */     for (int i = 0; i < this.prec - n.length(); i++) {
/* 226 */       naro = naro.concat("0");
/*     */     }
/* 228 */     n = naro.concat(n);
/*     */     
/* 230 */     sb.append(getTimeWithoutNano()).append(".").append(n);
/* 231 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public final String toString() {
/* 235 */     if (this.prec == 0) {
/* 236 */       return getTimeWithoutNano();
/*     */     }
/* 238 */     return getTimeWithNano();
/*     */   }
/*     */   
/*     */   public final Timestamp getTimestamp() {
/* 242 */     long t = getTime().getTime();
/* 243 */     Timestamp ts = new Timestamp(t);
/* 244 */     ts.setNanos(getNano() * 1000);
/* 245 */     return ts;
/*     */   }
/*     */   
/*     */   int getBit(byte[] x, int start, int len) {
/* 249 */     int xx0 = 0;
/* 250 */     int start_bit = start & 0x7;
/* 251 */     int s = start >> 3;
/* 252 */     int e = (start + len - 1 >> 3) + 1;
/* 253 */     int n = e - s;
/* 254 */     int p = start >> 3;
/*     */     
/* 256 */     if (n == 1) {
/* 257 */       byte[] t1 = new byte[4];
/* 258 */       System.arraycopy(x, p, t1, 0, 1);
/* 259 */       xx0 = Convertion.fourByteToInt(t1);
/* 260 */     } else if (n == 2) {
/* 261 */       byte[] t1 = new byte[4];
/* 262 */       System.arraycopy(x, p, t1, 0, 1);
/* 263 */       int x1 = Convertion.fourByteToInt(t1);
/*     */       
/* 265 */       System.arraycopy(x, p + 1, t1, 0, 1);
/* 266 */       int x2 = Convertion.fourByteToInt(t1);
/* 267 */       x2 = x2 << 8 & 0xFFFFFFFF;
/* 268 */       xx0 = x1 + x2;
/* 269 */     } else if (n == 3) {
/* 270 */       byte[] t1 = new byte[4];
/* 271 */       System.arraycopy(x, p, t1, 0, 1);
/* 272 */       int x1 = Convertion.fourByteToInt(t1);
/*     */       
/* 274 */       System.arraycopy(x, p + 1, t1, 0, 1);
/* 275 */       int x2 = Convertion.fourByteToInt(t1);
/* 276 */       x2 = x2 << 8 & 0xFFFFFFFF;
/*     */       
/* 278 */       System.arraycopy(x, p + 2, t1, 0, 1);
/* 279 */       int x3 = Convertion.fourByteToInt(t1);
/* 280 */       x3 = x3 << 16 & 0xFFFFFFFF;
/* 281 */       xx0 = x1 + x2 + x3;
/*     */     }
/*     */     
/* 284 */     return xx0 >> start_bit & -1 >>> 32 - len;
/*     */   }
/*     */   
/*     */   void setBit(byte[] x, int start, int len, int data)
/*     */   {
/* 289 */     int start_bit = start & 0x7;
/* 290 */     int last_bit = start + len & 0x7;
/* 291 */     int s = start >> 3;
/* 292 */     int e = (start + len - 1 >> 3) + 1;
/* 293 */     data <<= start_bit;
/* 294 */     byte[] p = Convertion.IntToByteArray(data);
/*     */     
/* 296 */     for (int n = s; n < e; n++) {
/* 297 */       if (n == s) {
/* 298 */         x[n] = ((byte)(x[n] & 255 >>> 8 - start_bit));
/* 299 */         x[n] = ((byte)(x[n] | p[(n - s)]));
/* 300 */       } else if (n == e - 1) {
/* 301 */         x[n] = ((byte)(x[n] & 255 << last_bit));
/* 302 */         x[n] = ((byte)(x[n] | p[(n - s)]));
/*     */       } else {
/* 304 */         x[n] = p[(n - s)];
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static byte[] TimeEncodeFast(byte[] dt)
/*     */   {
/* 313 */     byte[] ret = new byte[5];
/*     */     
/* 315 */     byte hour = dt[4];
/* 316 */     byte min = dt[5];
/* 317 */     byte sec = dt[6];
/* 318 */     int msec = (0xFF & dt[7]) + ((dt[8] & 0xFF) << 8) + (
/* 319 */       (dt[9] & 0xFF) << 16);
/*     */     
/*     */ 
/* 322 */     ret[0] = ((byte)(hour | (min & 0x7) << 5));
/*     */     
/*     */ 
/* 325 */     ret[1] = ((byte)((min & 0x38) >> 3 | (sec & 0x1F) << 3));
/*     */     
/*     */ 
/* 328 */     ret[2] = ((byte)((sec & 0x20) >> 5 | (msec & 0x7F) << 1));
/*     */     
/*     */ 
/* 331 */     ret[3] = ((byte)(msec >> 7 & 0xFF));
/*     */     
/*     */ 
/* 334 */     ret[4] = ((byte)(msec >> 15 & 0xFF));
/*     */     
/* 336 */     return ret;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\sql\DmdbTime.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */